/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2021-12-01 08:30:22
 * 天凉好个秋
 */
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 6000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code == 1) {
      return res
    } else {
      Message({
        message: res.msg || '操作失败',
        type: 'error',
        duration: 2000
      })
      return Promise.reject(new Error(res.msg || 'Error'))
    }
  },
  error => {
    Message({
      message: '连接超时',
      type: 'error',
      duration: 6000
    })
    return Promise.reject(error)
  }
)
export default service
